home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / LGP250S1.ZIP / src / libgplus.5 / libgplus / test-ins / ex_bar.cc < prev    next >
C/C++ Source or Header  |  1991-11-26  |  2KB  |  124 lines

  1. // bar.cc - test the runtime support for GNU C++ exception handling.
  2. //          inspired by a PROLOG toplevel loop by Heinz Seidl (hgs@cygnus.com)
  3.  
  4. #define S(s) SS(s)
  5. #define SS(s) # s
  6.  
  7. extern "C" {
  8.     int printf (const char*, ...);
  9.     void abort ();
  10.     int exit (int);
  11. };
  12.  
  13. enum TERM { ATOM, INTEGER, DOUBLE, STRUCTURE, LIST, REF, EOF };
  14.  
  15. exception {
  16.     int number;
  17.     char * message;
  18. } EX_IMPL;
  19.  
  20. exception {
  21.     TERM Term;
  22.     char * message;
  23. } EX_CALL;
  24.  
  25. exception {} EX_EOF;
  26.  
  27.  
  28. void pread (TERM & Term)
  29. {
  30.     // simulate an input buffer
  31.     //
  32.     static int p = 0;
  33.     TERM buffer[] = 
  34.       { REF,
  35.     LIST,
  36.     STRUCTURE,
  37.     DOUBLE,
  38.     INTEGER,
  39.     ATOM
  40.       };
  41.     const int blen = sizeof (buffer) / sizeof (TERM);
  42.     
  43.     if ( p < blen)
  44.       Term = buffer[p++];
  45.     else
  46.       Term = EOF;
  47. }
  48.  
  49. void call (const TERM & Term) raises EX_CALL, EX_EOF, EX_IMPL
  50. {
  51.     switch (Term) 
  52.       {
  53.     case REF:
  54.       raise EX_CALL (REF,
  55.              "Sorry - dereferencing not implemented (REF).");
  56.       break;
  57.     case LIST:
  58.       raise EX_CALL (LIST,   
  59.              "LISTs are not callable (LIST).");
  60.       break;
  61.     case STRUCTURE:
  62.       raise EX_CALL (STRUCTURE,
  63.              "Undefined predicate (STRUCTURE).");
  64.       break;
  65.     case DOUBLE:
  66.       raise EX_CALL (DOUBLE,
  67.              "DOUBLEs are not callable (DOUBLE).");
  68.       break;
  69.     case INTEGER:
  70.       raise EX_CALL (INTEGER,
  71.              "INTEGERs are not callable (INTEGER).");
  72.     case ATOM:
  73.        raise EX_CALL (ATOM,
  74.               "Undefined predicate (ATOM).");
  75.       break;
  76.     case EOF:
  77.       raise EX_EOF ();
  78.       break;
  79.     default:
  80.       raise EX_IMPL ( Term,
  81.              "Implementation error in file " __FILE__ 
  82.              " at line "  S(__LINE__) " .");
  83.       }
  84. }
  85.  
  86. void main()
  87. {
  88.     try {
  89.     while (1) {
  90.         try {
  91.         while (1) 
  92.           {
  93.               TERM Term;
  94.               pread (Term);
  95.               call (Term);
  96.           }
  97.         } except ep {
  98.         EX_CALL {
  99.             printf ("EXCEPTION(%d) : %s\n", ep.Term, ep.message);
  100.         }
  101.         EX_EOF {
  102.             printf ("EOF encountered.\n");
  103.             raise ep;
  104.         }
  105.         default {
  106.             raise ep;
  107.         }
  108.         }
  109.     }
  110.     } except ep {
  111.     EX_IMPL {
  112.         printf ("FATAL(%d): %s\n", ep.number, ep.message);
  113.         abort();
  114.     }
  115.     EX_EOF {
  116.         printf ("Good bye.\n");
  117.     }
  118.     default {
  119.         raise ep;
  120.     }
  121.     }
  122.     return 0;
  123. }
  124.